//
//  CJMYCompanyController.m
//  Boss
//
//  Created by admin on 2018/9/14.
//  Copyright © 2018年 admin. All rights reserved.
//

#import "CJMYCompanyController.h"
#import "CJCompanyMemberCell.h"
#import "CJCompanyIntroductionCell.h"
#import "CJMYCompanyHeaderCell.h"
#import "WYEditSectionView.h"
#import "WYProductCell.h"
#import "WYHonorCell.h"
#import "CJCompanyModel.h"
#import "CJEditFirmController.h"
#import "CJAllCompanyUserController.h"
@interface CJMYCompanyController ()<UITableViewDelegate,UITableViewDataSource,WYEditSectionViewDelegate,WYHonorCellDelegate>
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)CJCompanyModel *model;
@end
@implementation CJMYCompanyController
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    
//    UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"mine"] style:(UIBarButtonItemStyleDone) target:self action:nil];
//    UIBarButtonItem *item1 = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"mine"] style:(UIBarButtonItemStyleDone) target:self action:nil];
  //  self.navigationItem.rightBarButtonItems = @[item,item1];
   // self.navigationItem.rightBarButtonItem = item;
    
    //[self wr_setNavBarBackgroundAlpha:0];
    
    [self loadData];
    
}

-(void)loadData
{
    
    WEAKSELF
    
    [WYHttpServer getMyFirmStatusSucessBlock:^(BOOL success, id response, NSString *errorMessage) {
       
        if (success) {
            weakSelf.model = [CJCompanyModel getCompanyModelFromJson:response[@"result"]];
            [weakSelf.tableView reloadData];
        }
    }];
    
    
    
}




-(void)setupUI
{
    //self.navigationItem.title = @"我的公司";
    [self.view addSubview:self.tableView];
    
    self.tableView.estimatedRowHeight = 100;
    
    [self.tableView registerNib:[UINib nibWithNibName:@"CJCompanyMemberCell" bundle:nil] forCellReuseIdentifier:@"CJCompanyMemberCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"CJCompanyIntroductionCell" bundle:nil] forCellReuseIdentifier:@"CJCompanyIntroductionCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"CJMYCompanyHeaderCell" bundle:nil] forCellReuseIdentifier:@"CJMYCompanyHeaderCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"WYHonorCell" bundle:nil] forCellReuseIdentifier:@"WYHonorCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"WYProductCell" bundle:nil] forCellReuseIdentifier:@"WYProductCell"];
    
   // [self wr_setNavBarBarTintColor:kColorMainColor];
    UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"Home_page_share_icon"] style:(UIBarButtonItemStyleDone) target:self action:@selector(clickShareAction)];
    UIBarButtonItem *item1 = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"editConpanyMessage"] style:(UIBarButtonItemStyleDone) target:self action:@selector(clickEditAction)];
    self.navigationItem.rightBarButtonItems = @[item1];
    
    // 设置初始导航栏透明度
    [self wr_setNavBarBackgroundAlpha:0];
   
    // 设置导航栏按钮和标题颜色
    [self wr_setNavBarTintColor:[UIColor grayColor]];
    
}

-(void)clickShareAction
{
    
}
-(void)clickEditAction
{
    CJEditFirmController *efc = [[CJEditFirmController alloc]init];
    efc.model = self.model;
    [self.navigationController pushViewController:efc animated:YES];
}



- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat offsetY = scrollView.contentOffset.y;
    if (offsetY > self.navHeight)
    {
        CGFloat alpha = (offsetY - self.navHeight) / self.navHeight;
        [self wr_setNavBarBackgroundAlpha:alpha];
        [self wr_setNavBarTintColor:[[UIColor whiteColor] colorWithAlphaComponent:alpha]];
        [self wr_setNavBarTitleColor:[[UIColor whiteColor] colorWithAlphaComponent:alpha]];
       // [self wr_setStatusBarStyle:UIStatusBarStyleLightContent];
        self.title = self.model.firm.firmName;
    }
    else
    {
        [self wr_setNavBarBackgroundAlpha:0];
        [self wr_setNavBarTintColor:[UIColor grayColor]];
        [self wr_setNavBarTitleColor:[UIColor grayColor]];
     //   [self wr_setStatusBarStyle:UIStatusBarStyleDefault];
        self.title = @"";
    }
}



-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 5;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section==4) {
        return self.model.firmProduct.count;
    }
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section==0) {
        CJMYCompanyHeaderCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CJMYCompanyHeaderCell" forIndexPath:indexPath];
        if (!cell) {
            cell =[[NSBundle mainBundle]loadNibNamed:@"CJMYCompanyHeaderCell" owner:nil options:nil].firstObject;
        }
        if (self.model) {
            [cell setModel:self.model.firm];
        }
        
        
        
        return cell;
    }
    if (indexPath.section==1) {
        CJCompanyIntroductionCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CJCompanyIntroductionCell" forIndexPath:indexPath];
        if (!cell) {
            cell = [[NSBundle mainBundle]loadNibNamed:@"CJCompanyIntroductionCell" owner:nil options:nil].firstObject;
        }
        if (self.model) {
            cell.jieshaoLabel.text = self.model.firm.firmIntroduction;
        }
        return cell;
    }
    
    if (indexPath.section==2) {
        CJCompanyMemberCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CJCompanyMemberCell" forIndexPath:indexPath];
        if (!cell) {
            cell = [[NSBundle mainBundle]loadNibNamed:@"CJCompanyMemberCell" owner:nil options:nil].firstObject;
        }
        if (self.model) {
            [cell setDataArray:self.model.userList];
        }
        return cell;
    }
    if (indexPath.section==4) {
        WYProductCell *cell =[tableView dequeueReusableCellWithIdentifier:@"WYProductCell" forIndexPath:indexPath];
        if (!cell) {
            cell =[[NSBundle mainBundle]loadNibNamed:@"WYProductCell" owner:nil options:nil].firstObject;
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
//        WYProductService *service = self.dataArray[indexPath.row];
//        [cell setService:service];
        CJFirmProductModel *model = self.model.firmProduct[indexPath.row];
        [cell setModel:model];
        return cell;
    }
    if (indexPath.section==3) {
        WYHonorCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WYHonorCell" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.dataArray = self.model.firmHonor.mutableCopy;
        cell.delegate = self;
        return cell;
    }
    CJCompanyMemberCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CJCompanyMemberCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[NSBundle mainBundle]loadNibNamed:@"CJCompanyMemberCell" owner:nil options:nil].firstObject;
    }
    return cell;
    
    
    return nil;
}


-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section==0) {
        return 0.00001;
    }
    if (section==1) {
        return 0;
    }
    if (section==2) {
        return 50;
    }
    if (section==3) {
        return 50;
    }
    if (section==4) {
        return 50;
    }
    return 10;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section ==2) {
        WYEditSectionView *esc = [[WYEditSectionView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 44)];
       // esc.index = 3;
        if (self.model) {
            esc.label.text =[NSString stringWithFormat:@"公司成员（%@）",self.model.userNum];
        }
        esc.lineView.hidden = YES;
        esc.delegate = self;
        esc.backgroundColor = [UIColor whiteColor];
        //        if (self.honorArray.count) {
        //            esc.lineView.hidden = YES;
        //        }
        esc.moreBtn.hidden = YES;
        
        UIButton *allBtn = [[UIButton alloc]init];
        [esc addSubview:allBtn];
        
        [allBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(-16);
            make.centerY.mas_equalTo(esc);
        }];
        
        [allBtn setTitle:@"全部 >" forState:UIControlStateNormal];
        [allBtn setTitleColor:[UIColor hexStringToColor:@"#999999" alpha:1.0] forState:(UIControlStateNormal)];
        allBtn.titleLabel.font = [UIFont systemFontOfSize:12];
        
        [allBtn addTarget:self action:@selector(clickAllUserAction) forControlEvents:(UIControlEventTouchUpInside)];
        
        
        
        
        return esc;
    }
    
    
    
    
    if (section==3) {
        WYEditSectionView *esc = [[WYEditSectionView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 44)];
       // esc.index = 3;
        
        if (self.model) {
            esc.label.text =[NSString stringWithFormat:@"公司荣誉（%@）",self.model.firmHonorNum];
        }
        
        esc.delegate = self;
        esc.backgroundColor = [UIColor whiteColor];
//        if (self.honorArray.count) {
//            esc.lineView.hidden = YES;
//        }
        esc.moreBtn.hidden = YES;
        return esc;
        
    }
    if (section==4) {
        WYEditSectionView *esc = [[WYEditSectionView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 44)];
        esc.delegate = self;
        //esc.index = 4;
        if (self.model) {
            esc.label.text =[NSString stringWithFormat:@"产品服务（%@）",self.model.firmProductNum];
        }
        esc.backgroundColor = [UIColor whiteColor];
//        if (self.dataArray.count) {
//            esc.lineView.hidden = YES;
//        }
        esc.moreBtn.hidden = YES;
        return esc;
    }
    
    
    UIView *view = [[UIView alloc]init];
    view.backgroundColor = kColore2e2e2;
    return view;
}

-(void)clickAllUserAction
{
    CJAllCompanyUserController *auc = [[CJAllCompanyUserController alloc]init];
    auc.firmID = self.model.firm.id;
    [self.navigationController pushViewController:auc animated:YES];
}



- (UITableView *)tableView
{
    if (_tableView == nil) {
        CGRect frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
        _tableView = [[UITableView alloc] initWithFrame:frame
                                                  style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView setSeparatorStyle:(UITableViewCellSeparatorStyleNone)];
    }
    return _tableView;
}




@end
